'''
爬取数据的类
'''
import requests
from bs4 import BeautifulSoup

class Scrawler():
    def __init__(self):
        self.headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
        }

    def getCodes(self,url):
        rs = requests.get(url,headers=self.headers)
        rs.encoding = rs.apparent_encoding
        return rs.text

    '''获取文本'''
    def getTargetText(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),'html.parser')
        return [item.text for item in bs.select(selector=selector)]

    '''获取文本或图片链接'''
    def getTargetTextUrl(self, url, selector,choice):
        bs = BeautifulSoup(self.getCodes(url), 'html.parser')
        if choice ==0: # 获取文本链接
            return [item.attrs['href'] for item in bs.select(selector=selector)]
        elif choice ==1: #获取图链接
            return [item.attrs['src'] for item in bs.select(selector=selector)]

    '''获取图片'''
    def getTargetImg(self,imgurl,filename):
        rc = requests.get(imgurl,headers=self.headers).content
        with open(filename,'wb') as f:
            f.write(rc)
        return "ok"
    
    '''获取接口数据'''
    def getApiData(self,url):
        import json
        rs = requests.get(url,headers=self.headers).text
        if "var" in rs:
            eqindex = rs.index("=")+1
            rs = rs[eqindex:]
            print(rs)
        return json.loads(rs)


if __name__=='__main__':
    s = Scrawler()
    '''爬取文本测试'''
    url = "http://fund.eastmoney.com/api/static/FundCommpanyInfo.js?v=20230522151519"
    print(s.getApiData(url))
